﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.Devices;
using Small.Util.Model;

namespace Small.Util.IO
{
    public class DirectoryHandler
    {

        bool flag = false;
        /// <summary>
        /// 重命名文件夹
        /// </summary>
        /// <param name="oldName">旧文件夹名称</param>
        /// <param name="newName">现文件夹名称</param>
        /// <returns></returns>
        public bool RenameDirectory(string oldDirectoryName, string newDirectoryName)
        {
            flag = false;
            if (Directory.Exists(oldDirectoryName))
            {
                
                FileSystem.Rename(oldDirectoryName, newDirectoryName);
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 删除文件夹
        /// </summary>
        /// <param name="directoryName">文件夹名称</param>
        /// <returns></returns>
        public bool DeleteDirectory(string directoryName)
        {
            flag = false;

            if (Directory.Exists(directoryName))
            {
                Directory.Delete(directoryName, true);
                flag = true;
            } return flag;
        }
        /// <summary>
        /// 移动文件夹
        /// </summary>
        /// <param name="directoryName">文件夹名称</param>
        /// <param name="newDirectotyName">新的文件夹名称</param>
        /// <returns></returns>
        public bool MoveDirectory(string directoryName, string newDirectotyName)
        {
            flag = false;


            List<string> fileList = GetFiles(directoryName);
            foreach (var item in fileList)
            {

                if (!File.Exists(item)) { continue; }
                string sourceName = item;
                string newFileName = item.Replace(directoryName, newDirectotyName);
                string newFileParentDirectoty = Path.GetDirectoryName(newFileName);
                if (newDirectotyName == null) 
                {
                    return false;
                }

                Directory.CreateDirectory(newDirectotyName);
                File.Move(sourceName, newFileName);
            }
            Directory.Delete(directoryName);
            flag = true;

            return flag;
        }


        /// <summary>
        /// 获取到当前目录下的所有文件
        /// </summary>
        /// <param name="directoryName">文件夹名称</param>
        /// <returns>List<String>对象</returns>
        public List<String> GetFiles(string directoryName) 
        {
            List<string> list = new List<String>();
            if (!Directory.Exists(directoryName)) 
            {
                return list;
            }
            foreach (var item in Directory.GetFiles(directoryName))
            {
                list.Add(item);
            }
            foreach (var item in Directory.GetDirectories(directoryName))
            {
                GetFiles(item);
            }
            return list;
          
        }
       // private List<>

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="directoryName"></param>
        /// <returns></returns>
        public bool CreateDirectory(string directoryName)
        {
            flag = false;

            Directory.CreateDirectory(directoryName);
            return flag;
        }
    }
    
}
